Skip to content

Pyjoyment.Plugin.SecurityHeaders: csp_nonce, the nonce of the response - #638

Merged
dex4er merged 1 commit into
security-headers-pluginfrom
security-headers-nonce
Nov 18, 2026
Merged

dex4er merged 1 commit into
security-headers-pluginfrom
security-headers-nonce

Conversation

@dex4er

@dex4er dex4er commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Part of #634, its third step, stacked on #636.

Changes

  • Pyjoyment.Plugin.SecurityHeaders has the helper csp_nonce: 128 random bits in base64, made when first used and the same for the whole request.
    • The hook adds 'nonce-…' to default-src, script-src, script-src-elem, style-src and style-src-elem, in the dict policies of both csp and csp_report_only, wherever the policy has them. A policy without those directives restricts neither scripts nor styles, so it gets no nonce.
    • A response whose policy carries the nonce gets Cache-Control: private unless it already has a Cache-Control. A shared cache such as CloudFront would otherwise give the same nonce to every client, so it would no longer be secret. A page without inline code does not use the nonce and stays cacheable.
  • Pyjoyment.Plugin.TagHelpers: the inline forms of javascript and stylesheet add the nonce attribute when the application has a helper csp_nonce. That can be the plugin's helper or one of the application's own, for a CSP set by hand. Without one, the output is the same as before, so the tests ported from Mojolicious are unchanged. A script with a URL gets no nonce, as 'self' covers it; with 'strict-dynamic' the template passes nonce=c.csp_nonce().
  • Bundled templates (exception.html.ep, not_found.html.ep, debug.html.ep): their inline <style> and <script> use the tag helpers (# Unlike Mojolicious:), and the style attribute of the GitHub icon on the debug page moved to the stylesheet, as a nonce does not cover attributes. A strict policy therefore no longer blocks the error pages or the debug page.

Validation

  • t/Pyjoyment/security_headers_lite_app.py, subtest Nonce:
    • under a strict policy, every inline script and style of a page, of the 404 page and of the exception page in production and development mode (the latter is the debug page) has the nonce of the header, and no element has a style attribute
    • a new nonce for each response; Cache-Control: private is added, and a Cache-Control set by the action is kept
    • the report-only policy gets the nonce
    • the default policy gets no nonce and no Cache-Control
    • without the plugin, no nonce attribute
  • t/docs/Pyjoyment_Plugin_SecurityHeaders.py: the examples of csp_nonce
  • t/Pyjoyment/tag_helper_lite_app.py and t/docs/Pyjoyment_Plugin_TagHelpers.py unchanged and passing
  • pyright strict: 0 errors
  • PYTHONDEVMODE=1 python -m Pyjo.Prove -l -j 4 -r t on asyncio and on Pyjo.Reactor.Poll: all tests pass
  • sphinx-build -W passes
  • trunk check: no issues
  • CI

🤖 Generated with Claude Code

The helper csp_nonce makes a nonce of 128 random bits when it is first
used, and the hook adds it to the directives of the scripts and the styles
of the policies, and Cache-Control: private to a response without one, as
a shared cache would give the nonce to every client. The inline scripts and
styles of the tag helpers javascript and stylesheet get it when the
application has the helper, and the bundled templates use them, with no
style attribute, so that a strict policy does not block them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dex4er dex4er added this to the 0.4.1 milestone Nov 18, 2026
@dex4er
dex4er added this pull request to stack #637 November 18, 2026 16:54
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 Pyjoyment | 🛠️ Build #35990473 | 📁 Comparing 42cf49e against latest (4deb3f0)

  🔍 Preview build  

7 files changed · + 1 added · ± 6 modified

+ Added

± Modified

@github-actions

Copy link
Copy Markdown
Contributor

Docker Smoke Test

Job Tests Passed ✅ Skipped ⚠️ Failed ❌ Log
smoke (app) 11 11 0 0 ✅ 134971778743
smoke (fastcgi) 5 5 0 0 ✅ 134971778596
smoke (kubernetes) 8 8 0 0 ✅ 134971778752
smoke (lambda) 2 2 0 0 ✅ 134971778653
smoke (lambda-image) 5 5 0 0 ✅ 134971778732
smoke (lambda-stream) 2 2 0 0 ✅ 134971778077
smoke (lambda-zip) 9 9 0 0 ✅ 134971778779
smoke (lite) 4 4 0 0 ✅ 134971778610
smoke (lite-pep723) 4 4 0 0 ✅ 134971778523
All 50 50 0 0 ✅

The run of the workflow

@github-actions

Copy link
Copy Markdown
Contributor

Tests

Job Tests Passed ✅ Skipped ⚠️ Failed ❌ Log
prove (python 3.12, Asyncio) 7494 7465 29 0 ✅ 134971778436
prove (python 3.12, Poll) 7466 7435 31 0 ✅ 134971779040
prove (python 3.13, Asyncio) 7494 7465 29 0 ✅ 134971779024
prove (python 3.13, Poll) 7466 7435 31 0 ✅ 134971779493
prove (python 3.14, Asyncio) 7525 7501 24 0 ✅ 134971779476
prove (python 3.14, Poll) 7497 7471 26 0 ✅ 134971779018
prove (python 3.15, Asyncio) 7525 7501 24 0 ✅ 134971779053
prove (python 3.15, Poll) 7497 7471 26 0 ✅ 134971779042
prove (python 3.15t, Asyncio) 7525 7501 24 0 ✅ 134971778875
prove (python 3.15t, Poll) 7497 7471 26 0 ✅ 134971779054
prove (python mise, Asyncio) 7604 7569 35 0 ✅ 134971778745
prove (python mise, Asyncio, Windows) 7325 7259 66 0 ✅ 134971778043
prove (python mise, Poll) 7576 7539 37 0 ✅ 134971778444
prove (python mise, Poll, Windows) 7297 7229 68 0 ✅ 134971778582
prove (python pypy3.12, Asyncio) 7494 7465 29 0 ✅ 134971779192
prove (python pypy3.12, Poll) 7466 7435 31 0 ✅ 134971778848
pytest (python 3.12, ubuntu-26.04-arm) 444 418 26 0 ✅ 134971778778
pytest (python 3.15, ubuntu-26.04-arm) 444 423 21 0 ✅ 134971778609
pytest (python 3.15, windows-2025) 444 395 49 0 ✅ 134971778606
servers (python 3.12) 54 54 0 0 ✅ 134971779135
servers (python 3.15) 53 53 0 0 ✅ 134971778635
All 121187 120555 632 0 ✅

The run of the workflow

@dex4er dex4er modified the milestones: 0.4.1, 0.5.0 Nov 18, 2026
@dex4er
dex4er merged commit 6c16f8a into main Nov 18, 2026
36 checks passed
@dex4er
dex4er deleted the security-headers-nonce branch November 18, 2026 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant